"""
classifier combination by vote
"""
#how to import the module in the dir of the upper
import sys,os

sys.path.append("F:/program")
from LyricEmotion.pyweka.classiferresult import getDetailResult

class CompatibleException(Exception):pass
class ParaException(Exception):pass

def majorityrulelevel1(filelist):
    """
    all file are output of prediction which has a predict label only
    first check the compatible of files in the filelist
    """

def majorityrulelevel3(filelist,option="sum"):
    """
    files in filelist are output of prediction with the probability of each label
    first check the compatibility of the files in the filelist
    @param option sum/max/med
    """
    results = []
    for filename in filelist:
        results.append(getDetailResult(filename))
    if checkdetailcompatibility(results) == False:
        raise CompatibleException("majorityrulelevel3sum compatibility exception")
    """try:
        result = {
            "sum":majorityrulelevel3sum(results),
            "max":majorityrulelevel3max(results),
            "med":majorityrulelevel3med(results),            
            }[option]()
    except Exception,e:
        raise ParaException(option)
    """
    if option == "sum":
        result = majorityrulelevel3sum(results)
    elif option == "max":
        result = majorityrulelevel3max(results)
    else:
        raise ParaException(option)
    return result
    

def majorityrulelevel3sum(results):
    """
    files in filelist are output of prediction with the probability of each label
    first check the compatibility of the files in the filelist
    according to the sum
    """
    cresult = {}#combination result
    inin = results[0].keys()#instance index

    #initialize
    for number in inin:
        dic = {}
        dic['index'] = -1
        dic['trueclass'] = -1
        dic['predictclass'] = -1
        dic['dis'] = {}
        cresult[number] = dic
        
    for number in inin:
        cresult[number]['index'] = number
        cresult[number]['trueclass'] = results[0][number]['trueclass']
        diskeys = results[0][number]['dis'].keys()
        for k in diskeys:
            cresult[number]['dis'][k] = 0
            for eachresult in results:
                cresult[number]['dis'][k] += eachresult[number]['dis'][k]
            cresult[number]['dis'][k] /= len(results)
        mlabel = diskeys[0]
        for k in diskeys:
            if cresult[number]['dis'][k] > cresult[number]['dis'][mlabel]:
                mlabel = k
        cresult[number]['predictclass'] = mlabel
    
    return cresult
    

def majorityrulelevel3max(results):
    """
    files in filelist are output of prediction with the probability of each label
    first check the compatibility of the files in the filelist
    according to the max
    """

def majorityrulelevel3med(results):
    """
    files in filelist are output of prediction with the probability of each label
    first check the compatibility of the files in the filelist
    according to the median
    """

def checkdetailcompatibility(results):
    """
    @param results a list of detail result
    check:
    1.the number of instances
    2.the compatibility of the true label
    """
    return True
    
if __name__ == "__main__":
    filelist = [os.getcwd().replace("\\","/")+"/ensembledata/postfidf.txt",
                os.getcwd().replace("\\","/")+"/ensembledata/svsm.txt"]
    r = majorityrulelevel3(filelist,"sum")
    print r[8]
